package com.xz.courseschedule.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xz.courseschedule.common.vo.SemesterVo;
import com.xz.courseschedule.entity.Semester;
import com.xz.courseschedule.service.SemesterService;
import com.xz.courseschedule.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 学期管理控制器
 * @Author: zys
 * @CreateTime: 2025-05-22
 */
@Tag(name = "学期管理", description = "学期信息的增删改查管理")
@RestController
@RequestMapping("/semester")
public class SemesterController {

    @Resource
    private SemesterService semesterService;
    
    @Operation(summary = "获取学期列表", description = "获取系统中所有学期的详细信息列表")
    @GetMapping("/getList")
    public Result getList() {
        return semesterService.getList();
    }

    @Operation(summary = "获取学期名称集合", description = "获取所有学期的名称集合，用于下拉选择等场景")
    @GetMapping
    public Result queryAllSemester() {
        LambdaQueryWrapper<Semester> wrapper = new LambdaQueryWrapper<Semester>().select(Semester::getSemesterName);
        List<Semester> list = semesterService.list(wrapper);

        Set<String> set = list.stream().map(Semester::getSemesterName).collect(Collectors.toSet());

        return Result.success("成功返回学期集合", set);
    }

    @Operation(summary = "添加学期", description = "创建新的学期记录")
    @PostMapping
    public Result add(@RequestBody SemesterVo vo) {
        return semesterService.add(vo);
    }

    @Operation(summary = "更新学期", description = "更新指定学期的信息")
    @PutMapping
    public Result update(@RequestBody SemesterVo vo) {
        return semesterService.update(vo);
    }

    @Operation(summary = "删除学期", description = "根据ID删除指定的学期记录")
    @DeleteMapping("/{id}")
    public Result delete(@Parameter(description = "学期ID") @PathVariable Long id) {
        return semesterService.delete(id);
    }
}
